import pandas as pd
from snownlp import SnowNLP
from data_preprocessing import find_data, data_process, distinct_


def score_(txt):
    """
    利用snownlp进行情感分析
    txt：文本
    """
    s = SnowNLP(txt)
    return s.sentiments


def judge_label(score):
    """
    生成标签
    """
    if score >= 0.5:
        return 1
    else:
        return 0


def run_sentiment():
    # 读取文件
    df = pd.read_csv('G:/毕业设计/myspider_old/comment/winona.txt', header=None, names=['title', 'user', 'times', 'comment'])

    # 找到有关祛痘的商品
    df['new_title'] = df['title'].apply(lambda x: find_data(x, '祛痘'))

    df.dropna(axis=0, subset=['new_title'], inplace=True)
    del df['new_title']

    # 数据预处理
    df['comment'] = df['comment'].apply(lambda x: distinct_(x))
    df['comment'] = df['comment'].apply(lambda x: data_process(x))
    df.dropna(axis=0, subset=['comment'], inplace=True)

    # 情感分析
    df['score'] = df['comment'].apply(lambda x: score_(x))

    positive = df[df['score'] >= 0.7]
    negtive = df[df['score'] <= 0.3]

    positive['label'] = positive['score'].apply(lambda x: judge_label(x))
    negtive['label'] = negtive['score'].apply(lambda x: judge_label(x))

    # 输出文件
    positive[['comment', 'label']].to_csv(r'G:\毕业设计\情感分析\corpus\positive.txt', header=None)
    negtive[['comment', 'label']].to_csv(r'G:\毕业设计\情感分析\corpus\negtive.txt', header=None)


if __name__ == '__main__':
    run_sentiment()
